const AnchorMapper = require("../database/mapper/anchor");
const UserMapper = require("../database/mapper/user");
const {sleep} = require("ee-bin/tools/serve");
const {checkCookieField, checkCookieIsExpire} = require("../utils/fun");
const RedPacketBo = require("./red-packet-bo");

module.exports = class Scanf{
    status = false
    channel = null
    sender = null
    /**
     * @type RedPacketBo
     */
    sentinel = null
    setChannel(channel){
        this.channel = channel
        return this
    }
    setSender(sender){
        this.sender = sender
        return this
    }
    constructor(anchor_id) {
        this.anchorMapper = new AnchorMapper()
        this.userMapper = new UserMapper()
        this.anchor_id = anchor_id
    }

    sendMessage(message){
        this.sender.send(this.channel, {
            message: message,
            id: this.anchor_id
        })
    }

    async run(){
        this.status = true
        const anchor = await this.anchorMapper.read(this.anchor_id)
        let silent_gap = anchor.silent_gap * 1000
        let active_gap = anchor.active_gap * 1000
        let time = silent_gap
        let userList = await this.userMapper.getList({
            anchor_id:this.anchor_id,
            game_id:anchor.game_id
        })
        let userBoList = this.getRedPacketBoList(userList,anchor)
        if(!this.sentinel){
            this.status = false
            this.sendMessage("哨兵设置错误")
        }
        return
        let i = 0
        while (this.status){
            i!=0 && await sleep(time)
            i++
            let maps
            try {
                maps = await this.sentinel.getList()
            }catch (e){
                this.sendMessage(e.message + ":" +e.stack)
                break
            }
            if(Object.keys(maps).length == 0){
                this.sendMessage("没有新的红包")
                time = silent_gap
                continue
            }else{
                if(maps['24H']){
                    this.sendMessage("发现新人红包！")
                }
                if(maps['3D']){
                    this.sendMessage("发现3天红包！")
                }
                if(maps['7D']){
                    this.sendMessage("发现7天红包！")
                }
                if(maps['0']){
                    this.sendMessage("发现老粉红包！")
                }
                time = active_gap
            }
            for (let userBo of userBoList){
                userBo.receiveByMps(maps)
            }
        }
        this.sendMessage("任务已结束")
    }

    getRedPacketBoList(userList,anchor){
        let userBoList = []
        for(let user of userList){
            if(checkCookieField(user.cookies_obj) && !checkCookieIsExpire(user.cookies_obj)){
                let roleInfo = user.role_fmt_info
                let bo = new RedPacketBo(
                    roleInfo.game_id,
                    roleInfo.server_id,
                    roleInfo.role_id,
                    roleInfo.user_name)
                bo.setCookies(user.cookies)
                    .setClientIp(user.client_ip)
                    .setDeviceInfo(user.device)
                    .setRegisteredAt(user.registered_at)
                    .setUserId(user.id)
                    .setPwd(anchor.pwd)
                    .setRechargeMoney(user.recharge_money)

                userBoList.push(bo)
                if(user.id == anchor.sentinel_id){
                    this.sentinel = bo
                    console.log(user.login_fmt_info)
                    // bo.doLoginToLottery(user.login_fmt_info['sessionId'])
                }
            }
        }
        return userBoList
    }

    stop(){
        this.status = false
        this.sendMessage("关闭任务")
    }

}